package com.hashcoding.eyes.module.utils;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;

import java.util.ArrayList;

/**
 * 资源文件工具类.
 *
 * @author markmjw
 */
public class ResourceUtil {
    private static final String TAG = "ResourceUtil";

    /**
     * Gets the color.
     *
     * @param resId the res id
     * @return the color
     */
    public static int getColor(Context context, int resId) {
        return context.getResources().getColor(resId);
    }

    /**
     * Gets the dimens.
     *
     * @param resId the res id
     * @return the dimens
     */
    public static float getDimens(Context context, int resId) {
        return context.getResources().getDimension(resId);
    }

    /**
     * Gets the string.
     *
     * @param resId the res id
     * @return the string
     */
    public static String getString(Context context, int resId) {
        return context.getResources().getString(resId);
    }

    /**
     * Gets the drawable.
     *
     * @param resId the res id
     * @return the drawable
     */
    public static Drawable getDrawable(Context context, int resId) {
        return context.getResources().getDrawable(resId);
    }

    /**
     * Gets the arrays.
     *
     * @param resId the res id
     * @return the arrays
     */
    public static String[] getStringArrays(Context context, int resId) {
        return context.getResources().getStringArray(resId);
    }

    /**
     * Gets the arrays.
     *
     * @param resId the res id
     * @return the arrays
     */
    public static int[] getIntArrays(Context context, int resId) {
        return context.getResources().getIntArray(resId);
    }

    /**
     * Gets the color state list.
     *
     * @param resId the res id
     * @return the color state list
     */
    public static ColorStateList getColorStateList(Context context, int resId) {
        return context.getResources().getColorStateList(resId);
    }

    /**
     * 通过资源字符串获取对应的资源ID
     *
     * @param resAllName
     * @return 资源ID，or -1
     */
    public static int getIdByName(Context context, String resAllName) {
        int resId = -1;
        try {
            if (!TextUtils.isEmpty(resAllName)) {
                int i = resAllName.lastIndexOf(":") + 1;
                int j = resAllName.lastIndexOf("/");

                String type = resAllName.substring(i, j);
                String resName = resAllName.substring(j + 1);
                String packName = context.getPackageName();

                resId = context.getResources().getIdentifier(resName, type, packName);
            }

        } catch (Exception e) {
            LogUtil.e(TAG, "Don't find the resources id by the string : " + resAllName);
        }

        return resId;
    }

    /**
     * 通过资源ID获取对应的资源字符串(包含包名)
     *
     * @param resId
     * @return 资源名称
     */
    public static String getAllNameById(Context context, int resId) {
        String resAllName = "";
        try {
            resAllName = context.getResources().getResourceName(resId);
        } catch (Exception e) {
            LogUtil.e(TAG, "Don't find the resources name by the id : " + resId);
        }

        return resAllName;
    }

    /**
     * 通过资源ID获取对应的资源字符串
     *
     * @param resId
     * @return 资源名称
     */
    public static String getNameById(Context context, int resId) {
        String resName = "";
        try {
            String resAllName = context.getResources().getResourceName(resId);

            if (!TextUtils.isEmpty(resAllName)) {
                int i = resAllName.lastIndexOf("/");

                resName = resAllName.substring(i + 1);
            }
        } catch (Exception e) {
            LogUtil.e(TAG, "Don't find the resources name by the id : " + resId);
        }

        return resName;
    }

    /**
     * 通过资源string array获取对应的颜色资源数组
     *
     * @param resId
     * @return 颜色数组
     */
    public static int[] getColorsByStringArray(Context context, int resId) {
        ArrayList<Integer> list = new ArrayList<>();
        TypedArray array = context.getResources().obtainTypedArray(resId);
        int count = array.length();
        for (int i = 0; i < count; i++) {
            list.add(array.getColor(i, Color.WHITE));
        }
        array.recycle();

        int size = list.size();
        int[] colors = new int[size];
        for (int i = 0; i < size; i++) {
            colors[i] = list.get(i);
        }
        return colors;
    }
}
